<?php

class Administrator_LoginController extends Zend_Controller_Action
{

    public function init() {
        $option=array( 
        	"layout" => "login", 
        	"layoutPath" => APPLICATION_PATH."/layouts/scripts/administrator" 
      	); 
      	Zend_Layout::startMvc($option); 
        
      	$this->view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
      	$this->view->headTitle("Login - " . _page_title); 
        
      	$this->view->headLink()->appendStylesheet(_BASE_URL . "/public/css/admin.style.css"); 
      	$this->view->headLink()->appendStylesheet(_BASE_URL . "/public/css/jquery.uniform.css");
      	$this->view->headLink()->appendStylesheet(_BASE_URL . "/public/css/osx.css");
      	 
      	$this->view->headScript()->appendFile(_BASE_URL . "/public/js/jquery.min.1.5.2.js");
      	$this->view->headScript()->appendFile(_BASE_URL . "/public/js/jquery.uniform.js");
      	$this->view->headScript()->appendFile(_BASE_URL . "/public/js/simple.modal.js");
      	$this->view->headScript()->appendFile(_BASE_URL . "/public/js/osx.js");
      	$this->view->headScript()->appendFile(_BASE_URL . "/public/js/dkfw.js");              
    }
    
    public function preDispatch() {	}	
    

    public function indexAction()
    {

    	if($this->getInvokeArg('bootstrap')->getResource('checkLogin')){
			$this->_redirect(_BASE_URL . '/administrator');
		}
    	
    	$loginform = new Administrator_Form_Login();
    	$this->view->loginform = $loginform;
    	
    	if ($this->_request->isPost()) {
    		$db = Zend_Registry::get('connectDB');
    		$auth = Zend_Auth::getInstance();
    		
    		$authAdapter = new Zend_Auth_Adapter_DbTable($db);
	        $authAdapter->setTableName('users')	        	
	                ->setIdentityColumn('username')
	                ->setCredentialColumn('password');
	                
			$uname = $this->_request->getParam('username');
	        $paswd = $this->_request->getParam('password');
	        
	        $authAdapter->setIdentity($uname);
	        $authAdapter->setCredential(md5($paswd));
	        
    		$result = $auth->authenticate($authAdapter);
	        $flag = false;
	        if ($result->isValid()) {
	            $data = $authAdapter->getResultRowObject(null, array('password'));
				
	            $auth->getStorage()->write($data);
	            $flag = true;
	        }
	        if ($flag == true) {
	            $this->_redirect('/administrator/users');
	        }
    	}
    }
    
    public function inAction(){
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
    	
    	$helper = new helper();
    	$helper->reset_error();
    	
    	$username = $helper->fast('wl-username', 1, '', 'Nhập tên đăng nhập.');
    	$password = $helper->fast('wl-password', 1, '', 'Nhật mật khẩu.');
    	
    	if($helper->error==''){
    		
    		$db = Zend_Registry::get('connectDB');
    		$auth = Zend_Auth::getInstance ();
    		
    		
    		
    		$authAdapter = new Zend_Auth_Adapter_DbTable($db);
    		$authAdapter->setTableName('users')	        	
	                ->setIdentityColumn('username')
	                ->setCredentialColumn('password');
	                
			$uname = $this->_request->getParam('wl-username');
	        $paswd = $this->_request->getParam('wl-password');
	        $authAdapter->setIdentity($uname);
	        $authAdapter->setCredential(md5($paswd));
	        
	        $result = $auth->authenticate($authAdapter);
	        if ($result->isValid()) {
	        	$data = $authAdapter->getResultRowObject(null, array('password'));
	        	$auth->getStorage()->write($data);
	        }
    		
    		$json['result'] = "ok";
	    	$json['title']  = "Thành công";
	    	$json['text']   = "Đã đăng nhập thành công";
	    	
    	}else {
	    	$json['result'] = "error";
	    	$json['title']  = "Lỗi";
	    	$json['text']   = "Vui lòng kiểm tra lại:" . $helper->error;
    	}
    	
    	echo Zend_Json::encode($json);
    }
    
	public function outAction(){
    	$auth = Zend_Auth::getInstance();
    	$auth->clearIdentity();
    	$this->getHelper('viewRenderer')->setNoRender();
    	$this->_helper->redirector('index');
    }
}

